<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/2
 * Time: 16:23
 */
class Commwapqpass extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function status()
    {
        $qid = $this->input->get('qid');
        if (isset($qid)&&is_numeric($qid)) $qid=(int)$qid;
        else $this->error();
        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])) $uid=(int)$_SESSION['uid'];
        else $this->error();

        $this->load->database();
        $qdata=$this->db->select('start,end,status,title,cover')->from('comm_qpass')
            ->where('qid='.$qid)
            ->get()->row_array();
        $udata=$this->db->select('status')->from('comm_qpass_user')
            ->where('qid='.$qid)
            ->where('uid='.$uid)
            ->get()->row_array();
        $user=$this->db->select('telNum')->from('mod_user')
            ->where('uid='.$uid)
            ->get()->row_array();
        $res = array('uid'=>$uid,'time'=>0,'title'=>$qdata['title'],'status'=>0,'timestatus'=>0,'href'=>'#','telhidden'=>1);
        $coverimgs = explode('|',$qdata['cover']);
        $resource_url = 'http://wap.nldou.com';
        $res['cover']['img1'] = isset($coverimgs[0])?$resource_url.$coverimgs[0]:'';
        $res['cover']['img2'] = isset($coverimgs[1])?$resource_url.$coverimgs[1]:'';
        $res['cover']['img3'] = isset($coverimgs[2])?$resource_url.$coverimgs[2]:'';

        if (empty($user['telNum'])) $res['telhidden']=0;
        /*-0:未提交审核，1:等待审核结果，2:审核通过，3:审核未通过*/
        /*-0:活动结束，1:活动未开始，2:活动已开始*/
        if (!empty($qdata)){
            $now = time();//现在的时间
            $diff = $qdata['start']-$now;//距活动开始时间
            if ($now>$qdata['end']){$this->success($res);}//活动已结束
            elseif ($diff>0) {$res['time']=$diff;$res['timestatus']=1;}//活动未开始
            else{$res['timestatus']=2;}//活动已开始
            if (!empty($udata)) {
                $res['status'] = $udata['status'];
                if ($res['status']==2&&$res['timestatus']==2) $res['href']='http://wap.nldou.com/comm/multiaudio.html?qid='.$qid.'&uid='.$uid;
            }//用户审核状态
        }

        $this->success($res);
    }

    public function apply()
    {
        $qid = $this->input->post('qid');
        if (isset($qid)&&is_numeric($qid)) $qid=(int)$qid;
        else $this->error();
        $uid = $this->input->post('uid');
        if (isset($uid)&&is_numeric($uid)) $uid=(int)$uid;
        else $this->error();
        $tel = $this->input->post('tel');
        if (!isset($tel)) $tel='';

        $this->load->database();
        $data = array(
            array('qid'=>30,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>37,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>38,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>39,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>40,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>41,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>42,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>43,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>44,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>45,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>46,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>47,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>48,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>49,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>50,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>51,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>52,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>53,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>54,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>55,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>56,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>57,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>58,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>59,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>60,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>61,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>62,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>63,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>64,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time()),
            array('qid'=>65,'uid'=>$uid,'status'=>2,'created'=>time(),'changed'=>time())
        );
        $this->db->insert_batch('comm_qpass_user',$data);
        if (!empty($tel)){
            $udata = array('telNum'=>$tel);
            $this->db->update('mod_user',$udata,'uid='.$uid);
        }
        $this->success();
    }

    public function checkpass()
    {
        $qid = $this->input->get('qid');
        if (!isset($qid)||!is_numeric($qid)) die('invalid request');

        $this->load->database();
        $qdata=$this->db->select('start,end,status')->from('comm_qpass')
            ->where('qid='.$qid)
            ->get()->row_array();
        if (empty($qdata)) die('invalid request');

        if(!isset($_SESSION)) session_start();
        if (isset($_SESSION['uid'])) $uid=(int)$_SESSION['uid'];
        else {redirect('http://api.nldou.com/ucenter/weixinlogin?pid=1238&page=&param=qid.'.$qid);exit();};


        $udata=$this->db->select('status')->from('comm_qpass_user')
            ->where('qid='.$qid)
            ->where('uid='.$uid)
            ->get()->row_array();

        $now = time();//现在的时间
        if ($now>$qdata['start']&&$now<$qdata['end']&&$udata['status']==2){
            redirect('http://wap.nldou.com/comm/multiaudio.html?qid='.$qid.'&uid='.$uid);
            exit();
        }else{
            redirect('http://api.nldou.com/ucenter/weixinlogin?pid=1238&page=&param=qid.'.$qid);
            exit();
        }

    }

    public function sharecallback()
    {
        $post = $this->input->post();
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error();
        else $uid = (int)$post['uid'];
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error();
        else $qid = (int)$post['qid'];
        if (!isset($post['type'])||empty($post['type'])) $this->error();
        else $type = $post['type'];

        $data=array();
        $data['uid']=$uid;
        $data['type']=$type;
        $data['data']['pid']=1238;
        $data['data']['page']='';
        $data['data']['param']='qid.'.$qid;
        $data['created']=time();

        $str = igbinary_serialize($data);
        $this->load->model("redis_model");
        $redis = $this->redis_model->getRedisInstance();
        $redis->rPush('shareoplist',$str);
        $this->success();
    }

}